package com.haoqizhe.kernel.elasticsearch.common.utils;

import com.haoqizhe.kernel.core.entity.PageQuery;
import com.haoqizhe.kernel.core.entity.PageView;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

/**
 * 处理 es 分页查询结果
 * @author haoqizhe.li
 * @date 2018/9/8
 **/
public class PageUtil {
    /**
     * <p>获取框架分页结果</p>
     * @param page es分页查询结果
     * @param pageable es分页查询参数
     * @return  
     * @author haoqizhe.li
     * @date 2018/9/8
     **/
    public static PageView getPageView(Page page, Pageable pageable){
        PageView pageView = new PageView<>();
        pageView.setPageNum(pageable.getPageNumber());
        pageView.setPageSize(pageable.getPageSize());
        pageView.setPages(page.getTotalPages());
        pageView.setTotal(page.getTotalElements());
        pageView.setList(page.getContent());
        return pageView;
    }

    /**
     * <p>获取框架分页结果</p>
     * @param page es分页查询结果
     * @param pageQuery 框架分页查询参数
     * @return
     * @author haoqizhe.li
     * @date 2018/9/8
     **/
    public static  PageView getPageView(Page page, PageQuery pageQuery){
        PageView pageView = new PageView<>();
        pageView.setPageNum(pageQuery.getPageNum());
        pageView.setPageSize(pageQuery.getPageSize());
        pageView.setPages(page.getTotalPages());
        pageView.setTotal(page.getTotalElements());
        pageView.setList(page.getContent());
        return pageView;
    }
}
